<template>
  <div class="orderPayment">
    <top :text="'支付页'"></top>
    <div style="padding:0px 10px;">
      <div class="orderDetailBg" style="margin-top: 56px;" :class="{h260:payType==2,h160:payType==0}">
        <div style="text-align: center;padding-top: 10px;padding: 0px 10px">
          <div style="border-bottom: 1px dashed #efefef;height: 40px;line-height: 40px;font-size: 14px;">
            请在20分钟内完成支付，否则该订单将被取消
          </div>
        </div>
        <!-- 余额支付 -->
        <div v-if="payType==0">
          <div class="c9" style="margin-left: 35px;margin-top: 10px;font-size: 14px;">
            订单编号:{{orderid}}
          </div>
          <div class="c9" style="margin-left: 35px;margin-top: 10px;font-size: 14px;">
            交易金额:{{payAmount}}
          </div>
          <div class="c9" style="margin-left: 35px;margin-top: 10px;font-size: 14px;">
            购买产品:{{coin||detail.name}}
          </div>
        </div>
      </div>
      <!-- 钱包支付 -->
      <div v-if='payType==1' style="margin-top: -160px;">
        <div class="c9" style="margin-left: 35px;margin-top: 10px;font-size: 14px;">
          订单编号:{{orderid}}
        </div>
        <div class="c9" style="margin-left: 35px;margin-top: 10px;font-size: 14px;">
          交易金额:{{payAmount}}元
        </div>
        <div class="c9" style="margin-left: 35px;margin-top: 10px;font-size: 14px;">
          支付地址:{{walletObj.address}}
        </div>
        <div style="position: relative;color: #999;margin-left: 35px;margin-top: 10px;font-size: 14px;">
          <span>收款地址:</span>
          <input id="foo" class="c9 fr fr15" :value="typeValue" style="border:none;outline: none;background:#fff;margin-left: 11px;    overflow: hidden;text-overflow: ellipsis;white-space: nowrap;"
            readonly>
          <div class="copyBtn" style="font-size: 13px">
            <copy :show="true" :value="11" :text="'复制'" :id="'#foo'"></copy>
          </div>
        </div>
      </div>
      <!-- 银行卡支付 -->
      <div v-if="payType==2" style="margin-top: -220px;">
        <div class="c9" style="margin-left: 35px;margin-top: 10px;font-size: 14px;">
          订单编号:{{orderid}}
        </div>
        <div class="c9" style="margin-left: 35px;margin-top: 10px;font-size: 14px;">
          交易金额:{{payAmount}}元
        </div>
        <div class="c9" style="margin-left: 35px;margin-top: 10px;font-size: 14px;">
          付款卡号:{{bankObj.count}}
        </div>
        <div class="c9" style="margin-left: 35px;margin-top: 10px;font-size: 14px;">
          收款人:陈海波
        </div>
        <div class="c9" style="margin-left: 35px;margin-top: 10px;font-size: 14px;">
          收款银行:中国建设银行
        </div>
        <div style="position: relative;color: #999;margin-left: 35px;margin-top: 10px;font-size: 14px;">
          <span>收款卡号:</span>
          <input id="foo" class="c9 fr fr15" value="6217001210029664974" style="border:none;outline: none;background:#fff;margin-left: 11px;"
            readonly>
          <div class="copyBtn" style="font-size: 13px">
            <copy :show="true" :value="11" :text="'复制'" :id="'#foo'"></copy>
          </div>
        </div>
      </div>
    </div>
    <div class="moneyBtn" @click="moneyBtn" v-if="payType==0">
      马上支付
    </div>
    <div class="moneyBtn" v-if="payType!=0" @click="sendSms">
      我已支付
    </div>

    <!-- 钱包支付   银行卡支付-->
    <div style="padding: 0px 15px;margin-top: 20px;" v-if="payType!=0">
      <div style="font-size: 13px;color: #ff6363;">
        <div>交易提醒</div>
        <div>1、请使用您已绑定的钱包、银行卡进行支付，避免资产丢失;</div>
        <div>2、请勿将你的资产转入非上方的地址内，避免造成资产损失</div>
        <div>3、当日累计3笔订单取消，则会限制当天投资功能。</div>
      </div>
    </div>


    <!-- 输入支付密码 -->
    <div class="tips" v-show="showTips">
      <div class="tips-top">
        <div style="text-align: center;height: 45px;line-height: 45px;border-bottom: 1px solid #f6f6f6;">
          <img src="../../../assets/close.png" alt="" width="15" @click="showTips=false">
          <span class="c3">请输入支付密码</span>
        </div>
        <div>
          <div class="tips-center">
            <li>
              <input type="password" v-model="pwd1" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd2" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd3" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd4" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd5" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd6" maxlength="1" @keyup="checkPwd" class="input">
            </li>
          </div>
          <div class="forgetPassWord" @click="$router.push({path:'/main/forgetPay'})">忘记密码?</div>
        </div>
      </div>
    </div>

    <!-- 支付密码错误弹框 -->
    <div class="errTips" v-if="errTips">
      <div class="errTipsBox">
        <div class="errTips-top">
          支付密码错误,请重试!
        </div>
        <div class="errTipsBtn">
          <span class="active" @click="retry">重试</span>
          <span @click="$router.push({path:'/main/forgetPay'})">忘记密码?</span>
        </div>
        <div style="margin-left: 30px;font-size: 13px;margin-top: 5px;color: #ff1010">(说明：只能重试3次)</div>
      </div>
    </div>
     <!-- 设置支付密码 -->
    <div class="setPassWorldTips" v-if="isPassWorld" @click="isPassWorld=false">
      <div class="setPasswordText">
        <div class="setPassWorldTitle">请设置支付密码!</div>
        <div class="goPassworld" @click="$router.push({path:'/main/setPayPassword'})">立即前往</div>
      </div>
    </div>
  </div>
</template>
<script>
  import Copy from '../../common/copy.vue'
  import Top from '../../common/top.vue'
  import http from '../../../service/http.js'
  import tips from '../../../service/tips.js'
  import util from '../../../service/util.js'
  export default {
    components: {
      Top,
      Copy
    },
    data() {
      return {
        payType: 0,//0表示余额支付 1钱包支付 2银行卡支付
        payAmount: "",
        coin: "",
        detail: {},
        orderid: "",
        showTips: false,//显示支付密码弹框
        errTips: false,//显示支付错误弹框
        pwd: "",
        pwd1: "",
        pwd2: "",
        pwd3: "",
        pwd4: "",
        pwd5: "",
        pwd6: "",
        walletObj: {},
        bankObj: {},
        typeValue: "",
        isPassWorld: ""
      }
    },
    mounted() {
      this.payType = this.$route.query.payType;
      if (this.payType == 1) {
        this.getWalletDefult();
      }
      if (this.payType == 2) {
        this.getBankDefult();
      }
      var coinTyps = this.$route.query.coinTyps;
      if (coinTyps == 'BTC') {
        this.typeValue = '1QGDMGUWDTYDcMFufuAHAzx3gZLWKWGVhD'
      } else {
        this.typeValue = '0x65e7Fd5479beB0D11C0073259E22809E2e3BEa6e'
      }
      this.orderid = this.$route.query.orderid;
      this.payAmount = this.$route.query.payAmount;
      this.detail = this.$store.getters.getData;
      var txts = document.querySelectorAll(".input");
      for (var i = 0; i < txts.length; i++) {
        var t = txts[i];
        t.index = i;
        t.setAttribute("readonly", true);
        t.onkeyup = function () {
          this.value = this.value.replace(/^(.).*$/, '$1');
          var next = this.index + 1;
          if (next > txts.length - 1) return;
          txts[next].removeAttribute("readonly");
          txts[next].focus();
        }
      }
      txts[0].removeAttribute("readonly");
    },
    methods: {
      // 获取默认银行卡地址
      getBankDefult() {
        http.get("/user/bank?type=defult").then((res) => {
          if (res.data.status == 200) {
            this.bankObj = res.data.body;
          }
        })
      },
      // 获取默认钱包地址
      getWalletDefult() {
        http.get("/user/address?type=defult").then((res) => {
          if (res.data.status == 200) {
            this.walletObj = res.data.body;
          }
        })
      },
      // 判断有无支付密码
      hasPay() {
        http.post('/user/hasPay', {
          username: window.localStorage.getItem("login.username")
        }).then((res) => {
          if (res.status == 200 && !res.data.body) {//无支付密码
            this.isPassWorld = true;
          }
          if (res.status == 200 && res.data.body) { //有支付密码
            this.showTips = true;
            this.errTips = false;
            this.isPassWorld = false;
          }
        })
      },
      //校验支付密码
      checkPwd() {
        this.pwd = this.pwd1 + this.pwd2 + this.pwd3 + this.pwd4 + this.pwd5 + this.pwd6;
        if (this.pwd1 && this.pwd2 && this.pwd3 && this.pwd4 && this.pwd5 && this.pwd6) {
          http.post('/user/authPay', {
            payPwd: util.md5(this.pwd)
          }).then((resultData) => {
            if (resultData.status == 200 && resultData.data.body) {//支付密码正确 如果是余额支付 调用付款的接口
              if (this.coin) {
                this.payOrder();
              } else {
                this.placeOrder();
              }
            } else {
              this.showTips = false;
              this.errTips = false;
              this.isPassWorld = false;
              this.pwd1 = ""
              this.pwd2 = ""
              this.pwd3 = ""
              this.pwd4 = ""
              this.pwd5 = ""
              this.pwd6 = ""
              tips.tips(resultData.data.msg);
            }
          }).catch((resultData) => {
            this.showTips = false;
            this.errTips = false;
            this.isPassWorld = false;
            this.pwd1 = ""
            this.pwd2 = ""
            this.pwd3 = ""
            this.pwd4 = ""
            this.pwd5 = ""
            this.pwd6 = ""
            tips.tips(resultData.data.msg);
          })
        }
      },
      //付款
      placeOrder() {
        http.post("/order/payOrder", {
          orderid: this.orderid
        }).then((res) => {
          if (res.status == 200) {
            this.showTips = false;
            this.errTips = false;
            this.isPassWorld = false;
            this.pwd1 = ""
            this.pwd2 = ""
            this.pwd3 = ""
            this.pwd4 = ""
            this.pwd5 = ""
            this.pwd6 = ""
            tips.tips(res.data.msg)
            setTimeout(() => {
              this.$router.push({ path: '/main/fp/otcList' })
            }, 3000);
          } else {
            this.showTips = false;
            this.errTips = false;
            this.isPassWorld = false;
            this.pwd1 = ""
            this.pwd2 = ""
            this.pwd3 = ""
            this.pwd4 = ""
            this.pwd5 = ""
            this.pwd6 = ""
            tips.tips(res.data.msg);
          }
        }).catch((res) => {
          this.showTips = false;
          this.errTips = false;
          this.isPassWorld = false;
          this.pwd1 = ""
          this.pwd2 = ""
          this.pwd3 = ""
          this.pwd4 = ""
          this.pwd5 = ""
          this.pwd6 = ""
          tips.tips(res.data.msg);
        })
      },
      // 数字资产支付
      payOrder() {
        http.post("/coins/pay", {
          orderid: this.orderid
        }).then((resPay) => {
          if (resPay.data.status == 200) {
            this.showTips = false;
            this.errTips = false;
            this.isPassWorld = false;
            this.pwd1 = ""
            this.pwd2 = ""
            this.pwd3 = ""
            this.pwd4 = ""
            this.pwd5 = ""
            this.pwd6 = ""
            tips.tips(resPay.data.msg);
            setTimeout(() => {
              this.$router.push({ path: '/main/fp/otcList' })
            }, 3000)
          } else {
            this.showTips = false;
            this.errTips = false;
            this.isPassWorld = false;
            this.pwd1 = ""
            this.pwd2 = ""
            this.pwd3 = ""
            this.pwd4 = ""
            this.pwd5 = ""
            this.pwd6 = ""
            tips.tips(resPay.data.msg);
          }
        }).catch((resPay) => {
          this.showTips = false;
          this.errTips = false;
          this.isPassWorld = false;
          this.pwd1 = ""
          this.pwd2 = ""
          this.pwd3 = ""
          this.pwd4 = ""
          this.pwd5 = ""
          this.pwd6 = ""
          tips.tips('系统异常');
        })
      },
      // 重试按钮
      retry() {
        this.errTips = false;
        this.showTips = true;
        this.pwd1 = ""
        this.pwd2 = ""
        this.pwd3 = ""
        this.pwd4 = ""
        this.pwd5 = ""
        this.pwd6 = ""
      },
      moneyBtn() {
        this.hasPay();
      },
      sendSms() {
        http.put("/order/sms/admin", {
          orderid: this.orderid,
          payAmount: this.payAmount + "元"
        }).then((res) => {
          if (res.data.status == 200) {
            tips.tips(res.data.msg);
            setTimeout((res) => {
              this.$router.push({ path: '/main/fp/otcList' })
            }, 3000)
          }
        })
      }
    },
  }
</script>
<style lang="less" scoped>
  .h260 {
    height: 260px !important;
  }

  .h160 {
    height: 160px !important;
  }

  .orderPayment {
    position: fixed;
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px;
    background: #f6f6f6;
  }

  .orderPayment .vux-header {
    position: relative;
    padding: 3px 0;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    background-image: linear-gradient(-135deg, #FEDF33 0%, #FE9D09 100%);
  }

  .c9 {
    color: #999;
  }

  .orderPayment .vux-header .vux-header-left .left-arrow:before {
    content: "";
    position: absolute;
    width: 12px;
    height: 12px;
    border: 1px solid #fff;
    border-width: 1px 0 0 1px;
    -webkit-transform: rotate(315deg);
    transform: rotate(315deg);
    top: 8px;
    left: 7px;
  }

  .orderDetailBg {
    background-image: url('../../../assets/zfBg.png');
    height: 200px;
    width: 100%;
    background-size: 100% 100%;
  }

  .orderPayment .copyBtn {
    color: #fea620;
    border: 1px solid #fea620;
    border-radius: 5px;
    width: 60px;
    text-align: center;
    height: 20px;
    line-height: 20px;
    position: absolute;
    right: 10px;
    top: 2px;
  }

  .orderPayment .moneyBtn {
    margin-top: 50px;
    width: 80%;
    margin-left: 10%;
    line-height: 40px;
    color: #fff;
    border-radius: 25px;
    height: 40px;
    text-align: center;
    background-image: linear-gradient(-135deg, #FEDF33 0%, #FE9D09 100%);
  }

  .navBtnetf {
    position: absolute;
    top: 15px;
    left: 15px;
    z-index: 888;
    transform: rotate(180deg);
  }

  .tips {
    position: fixed;
    background: rgba(0, 0, 0, 0.3);
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px;
    z-index: 999;
    .tips-top {
      position: absolute;
      top: 40%;
      height: 60%;
      background: #fff;
      width: 100%;
      border-bottom: 1px solid #f6f6f6;
      img {
        position: absolute;
        left: 15px;
        top: 15px;
      }
    }
  }

  .tips-center {
    padding-left: 10%;
    margin-top: 45px;
  }

  .tips-center li {
    float: left;
    width: 15%;
    list-style: none;
    height: 45px;
    line-height: 45px;
    border: 1px solid #ccc;
    border-right: none;
    overflow: hidden;
    text-align: center;
    input {
      border: none;
      outline: none;
      height: 45px;
      line-height: 45px;
      width: 100%;
      display: block;
      text-align: center;
    }
  }

  .tips-center li:last-child {
    border-right: 1px solid #ccc;
  }

  .forgetPassWord {
    position: absolute;
    right: 28px;
    top: 150px;
    color: #fea620;
  }

  .errTips {
    position: fixed;
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px;
    background: rgba(0, 0, 0, 0.3);
    z-index: 999;
  }

  .errTipsBox {
    position: absolute;
    top: 40%;
    width: 90%;
    height: 130px;
    background: #fff;
    margin-left: 5%;
    border-radius: 15px;
    .errTips-top {
      height: 60px;
      line-height: 60px;
      text-align: center;
      border-bottom: 1px solid #efefef;
    }
    .errTipsBtn {
      span {
        display: inline-block;
        width: 48%;
        text-align: center;
        margin-top: 15px;
        color: #fea620;
      }
      .active {
        border-right: 1px solid #efefef;
      }
    }
  }

  .setPassWorldTips {
    position: fixed;
    top: 0px;
    left: 0px;
    bottom: 0px;
    right: 0px;
    background: rgba(0, 0, 0, 0.3);
    z-index: 999;
  }

  .setPasswordText {
    position: absolute;
    top: 40%;
    height: 130px;
    background: #fff;
    width: 90%;
    margin-left: 5%;
    border-radius: 15px;
    .setPassWorldTitle {
      height: 60px;
      line-height: 60px;
      text-align: center;
      border-bottom: 1px solid #efefef;
      color: #000;
    }
    .goPassworld {
      height: 70px;
      line-height: 65px;
      color: #fea620;
      text-align: center;
    }
  }
</style>